package com.zez.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zez.entity.Classes;
import com.zez.entity.Student;
import com.zez.mapper.ClassesMapper;
import com.zez.mapper.StudentMapper;
import com.zez.service.StudentService;
import com.zez.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClassesMapper classesMapper;

    @Override
    public List<Student> findAll() {
        List<Student> students = studentMapper.selectByExample(null);
        students.forEach(st -> {
            Classes classes = classesMapper.selectByPrimaryKey(st.getCid());
            st.setCname(classes.getCname());
        });
        return students;
    }

    // 分页查询
    @Override
    public PageResult<Student> findByPage(Integer page, Integer pageSize) {
        // 1. 开启分页
        System.out.println(" 12312312312312");
        PageHelper.startPage(page, pageSize);

        // 2. 查询数据
        List<Student> students = this.findAll();
        System.out.println(" ytfvt");

        // 3. 获取分页信息
        PageInfo<Student> pageInfo = new PageInfo<>(students);

        // 4. 构建并返回 PageResult
        return new PageResult<>(pageInfo.getList(), pageInfo.getTotal());
    }
}
